package com.huika.renrench.utils;

import android.content.SharedPreferences;

import com.huika.renrench.GlobalApp;

/**
 * 数据储存 XML文件
 */
public class PreferencesHelper {

	private static SharedPreferences sp;
	private static SharedPreferences.Editor editor;
 
	private static PreferencesHelper mPreferencesHelper;
	public static final String NAME = "yuedian_preference";
	/**    
	 * 定位数据
	 */
	public static final String CURRENT_LATITUDE = "Latitude";
	public static final String CURRENT_LONGITUDE = "Longitude";
	public static final String CURRENT_LOCATION = "location";
	public static final String CURRENT_LOCATION01 = "location01";
	public static final String CURRENT_CITY = "city";
	public static final String CURRENT_PROVINCE_CITY_DISTRICT = "current_province_city_district";
	/**手动登录后最多维持14天自动登录有效期的规则*/
	static final String LOG_IN_EFFECTIVE_TIME = "log_in_effective_time";


	public static final String IMAGE_URI = "image_uri";// 存储登录界面的图像
	
	/** 版本更新*/
	public static final String LAST_VERSION_CHECK_TIME = "lastVersionCheckTime";
	public static final String VERSION_CODE = "currentVersionCode";// 当前版本号
	public static final String LAST_CHECK_VERSION = "lastCheckVersion";// 最新版本号


	public static final String ADVERTISEMENT_ID_TIME = "advertisement_id_time";

	public static final String IS_CURRENT_CHATTING = "is_current_chatting";

	/**听筒扬声器设置*/
	public static final String VOICE_SPEAKER_ON = "";

	/** 聊天消息草稿的保存 */
	public static final String MESSAGE_DRAFTS_CONSTANT_SAVED = "message_drafts_constant_saved";
	public static final String MESSAGE_DRAFTS_TIME_SAVED = "message_drafts_time_saved";

	public static final String SYN_CLOUD_SYSTEM_MSG_TIME = "syn_cloud_system_msg_time";
	public static final String DELTA_TIME = "delta_time";// 本地时间与服务端的时间差
	public static final String REFRESH_TIME = "refresh_time";// 刷新时间

	public static final String KEY_LOGIN_NAME = "key_login_name";
	public static final String KEY_LOGIN_PWD = "key_login_pwd";
	public static final String KEY_LOGIN_UID = "key_login_uid";
	public static final String KEY_IS_FINISH_USERINFO = "key_is_finish_userinfo";//是否完善资料
	public static final String KEY_IS_RELEASE_ENVIRONMENT = "key_is_release_environment";//正式环境key
	public static final String KEY_IS_RELEASE_TEST_ENVIRONMENT= "key_is_release_test_environment";//正式环境下的测试环境key
	public static final String KEY_IS_TEST_ENVIRONMENT = "key_is_test_environment";//测试环境key
	public static final String KEY_ENVIRONMENT ="key_environment";

	// Context context;
	// 消息提醒设置
	private static String REMIND_SETTING_PERSON_VOICE = "remind_setting_person_voice";
	private static String REMIND_SETTING_GROUP_VOICE = "remind_setting_group_voice";
	private static String REMIND_SETTING_PERSON_VIBRATE = "remind_setting_person_vibrate";
	private static String REMIND_SETTING_GROUP_VIBRATE = "remind_setting_group_vibrate";
	private static String RECEIVE_REMIND = "receive_remind";
	private static String RECEIVE_REMIND_SMS = "receive_remind_sms";
	private static String VOICE_REMIND = "voice_remind";
	private static String VIBRATE_REMIND = "vibrate_remind";

	private PreferencesHelper() {
		sp = GlobalApp.getInstance().getSharedPreferences(NAME, 0);
		editor = sp.edit();
	}

	public static void setVoiceSpeakerOn(boolean isOn) {
		editor.putBoolean(VOICE_SPEAKER_ON, isOn);
		editor.commit();
	}

	public static boolean getVoiceSpeakerOn() {
		return sp.getBoolean(VOICE_SPEAKER_ON, true);
	}

	synchronized public static PreferencesHelper getInstance() {
		if (null == mPreferencesHelper) {
			mPreferencesHelper = new PreferencesHelper();
		}
		return mPreferencesHelper;
	}

	/**
	 * 储存值
	 * 
	 * @param key
	 * @param value
	 */
	public static void setString(String key, String value) {
		editor = sp.edit();
		editor.putString(key, value);
		editor.commit();
	}

	public static void setInt(String key, int value) {
		editor = sp.edit();
		editor.putInt(key, value);
		editor.commit();
	}

	public static void setLong(String key, long value) {
		editor = sp.edit();
		editor.putLong(key, value);
		editor.commit();
	}

	public static void setBoolean(String key, boolean value) {
		editor = sp.edit();
		editor.putBoolean(key, value);
		editor.commit();
	}

	/**
	 * 获取值
	 * 
	 * @param key
	 * @return
	 */
	public static String getString(String key) {
		return sp.getString(key, "");
	}

	public static int getInt(String key) {
		return sp.getInt(key, -1);
	}

	public static long getLong(String key) {
		return sp.getLong(key, 0L);
	}

	public static boolean getBoolean(String key, boolean defaultValue) {
		return sp.getBoolean(key, defaultValue);
	}

	public void remove(String name) {
		editor.remove(name);

	}

	/**
	 * 方法概述：设置当前的纬度
	 */
	public static void setCurrentLatitude(Double state) {
		editor.putString(CURRENT_LATITUDE, state.toString());
		editor.commit();
	}


	/**
	 * 方法概述：设置当前的经度
	 */
	public static void setCurrentLongitude(Double state) {
		editor.putString(CURRENT_LONGITUDE, state.toString());
		editor.commit();
	}

	/**
	 * 方法概述：设置当前位置
	 */
	public static void setCurrentLocation(String location) {
		editor.putString(CURRENT_LOCATION, location);
		editor.commit();
	}
	/**
	 * 方法概述：设置当前位置,编辑资料使用
	 */
	public static void setCurrentLocation01(String location) {
		editor.putString(CURRENT_LOCATION01, location);
		editor.commit();
	}

	/**
	 * @description： 设置当前的城市(目前在酒店模块中用，选择城市后会有变化)
	 * @author ldm
	 * @date 2014年8月6日 下午5:40:09
	 */
	public static void setCurrentCityCut(String city) {
		editor.putString(CURRENT_CITY, city);
		editor.commit();  
	}

	/**
	 * @description：获取当前城市
	 * @author ldm
	 * @date 2014年8月6日 下午5:43:21
	 */
	public static String getCurrentCityCut() {
		return sp.getString(CURRENT_CITY, "深圳");
	}

	/**
	 * 方法概述：返回当前纬度
	 * 
	 */
	public static double getCurrentLatitude() {
		return Double.parseDouble(sp.getString(CURRENT_LATITUDE, "0"));
	}

	public static String getCurrentLocation() {
		return sp.getString(CURRENT_LOCATION, "");
	}
	/**编辑资料使用*/
	public static String getCurrentLocation01() {
		return sp.getString(CURRENT_LOCATION01, "深圳");
	}

	/**
	 * 方法概述：返回当前经度
	 */
	public static double getCurrentLongitude() {
		return Double.parseDouble(sp.getString(CURRENT_LONGITUDE, "0"));
	}

	public static void setImageUri(String imageUri) {
		editor.putString(IMAGE_URI, imageUri);
		editor.commit();
	}

	public static String getImageUri() {
		return sp.getString(IMAGE_URI, "");
	}

	/**
	 * @description：
	 */
	public static void setIsChatting(Boolean is) {
		editor.putBoolean(IS_CURRENT_CHATTING, is);
		editor.commit();
	}

	public static boolean getIsChatting() {
		return sp.getBoolean(IS_CURRENT_CHATTING, false);
	}

	/**
	 * @description：保存草稿功能
	 * @author lzt
	 * @date 2014年9月16日 下午2:14:23
	 */
	public static void setMessageDraftsContent(String drafts, long uid, long myuid) {
		editor.putString(MESSAGE_DRAFTS_CONSTANT_SAVED + uid + myuid, drafts);
		editor.commit();
	}

	public static String getMessageDraftsContent(long uid, long myuid) {
		return sp.getString(MESSAGE_DRAFTS_CONSTANT_SAVED + uid + myuid, "");
	}

	public static void setMessageDraftsTime(long time, long uid, long myuid) {
		editor.putLong(MESSAGE_DRAFTS_TIME_SAVED + uid + myuid, time);
		editor.commit();
	}

	public static long getMessageDraftsTime(long uid, long myuid) {
		return sp.getLong(MESSAGE_DRAFTS_TIME_SAVED + uid + myuid, 0);
	}
	
	/**
	 * @Description: 设置是完成否完善资料
	 * @param uid 登陆用户uid
	 * @param isFinish 是否已经完善资料
	 */
	public static void setIsFinishUserInfo(long uid, boolean isFinish) {
		editor.putBoolean(KEY_IS_FINISH_USERINFO + uid, isFinish);
		editor.commit();
	}

	public static boolean getIsFinishUserInfo(long uid) {
		return sp.getBoolean(KEY_IS_FINISH_USERINFO + uid, false);
	}

	/**
	 * 
	 * @description：保存云同步系统时间 
	 * @author lzt
	 * @date 2014年12月22日 下午3:57:15
	 */
	public static void setSynCloudSystemMsgTime(long time, long myuid) {
		editor.putLong(SYN_CLOUD_SYSTEM_MSG_TIME + myuid, time);
		editor.commit();
	}

	public static long getSynCloudSystemMsgTime(long myuid) {
		return sp.getLong(SYN_CLOUD_SYSTEM_MSG_TIME + myuid, 0);
	}

	public static void setDeltaTime(long time, long myuid) {
		editor.putLong(DELTA_TIME + myuid, time);
		editor.commit();
	}

	public static long getDeltaTime(long myuid) {
		return sp.getLong(DELTA_TIME + myuid, 0);
	}

	public static void setlogInEffectiveTime(long time) {
		editor.putLong(LOG_IN_EFFECTIVE_TIME, time);
		editor.commit();
	}

	public static long getlogInEffectiveTime() {
		return sp.getLong(LOG_IN_EFFECTIVE_TIME, 0);
	}

	public static void setRefreshTime(long time) {
		editor.putLong(REFRESH_TIME, time);
		editor.commit();
	}

	public static long getRefreshTime() {
		return sp.getLong(REFRESH_TIME, System.currentTimeMillis());
	}

	/**
	 * @Description:保存用户名和密码
	 * @param account
	 * @param pwd
	 */
	public void saveLoginInfo(String account, String pwd) {
		editor.putString(KEY_LOGIN_NAME, account);
		editor.putString(KEY_LOGIN_PWD, pwd);
		editor.commit();
	}
	
	public String getLoginInfo (){
		return sp.getString(KEY_LOGIN_PWD, "");
	}

	// 设置新消息通知接收
	public static void setReceiveRemind(boolean paramBoolean) {
		editor.putBoolean(RECEIVE_REMIND, paramBoolean);
		editor.commit();
	}

	public static boolean getReceiveRemind() {
		return sp.getBoolean(RECEIVE_REMIND, true);
	}
	// 设置短信通知接收
	public static void setReceiveRemindSMS(boolean paramBoolean) {
		editor.putBoolean(RECEIVE_REMIND_SMS, paramBoolean);
		editor.commit();
	}
	
	public static boolean getReceiveRemindSMS() {
		return sp.getBoolean(RECEIVE_REMIND_SMS, true);
	}

	// 设置铃声提醒
	public static void setVoiceRemind(boolean paramBoolean) {
		editor.putBoolean(VOICE_REMIND, paramBoolean);
		editor.commit();
	}

	public static boolean getVoiceRemind() {
		return sp.getBoolean(VOICE_REMIND, true);
	}

	// 震动提醒
	public static void setVibrateRemind(boolean paramBoolean) {
		editor.putBoolean(VIBRATE_REMIND, paramBoolean);
		editor.commit();
	}

	public static boolean getVibrateRemind() {
		return sp.getBoolean(VIBRATE_REMIND, true);
	}
 
	/**
	 * @Description:清除相关登陆标示
	 *  
	 */
	public static void clearLoginState() {
		editor.putString(KEY_LOGIN_PWD, "");
		editor.putLong(KEY_LOGIN_UID, 0);
		editor.commit();
	}

}
